(function() {
  var express, iced, pgsql, router, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  uuid = require('uuid');

  router = express.Router();

  module.exports = router;

  router.all('/', function(req, res, next) {
    var chnl, client, done, end_date, err, fmt_pan, incoming, index, limit_index, offset_index, params, record, result, settl, start_date, succed, table_name, today, trcode, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    where_clause = "(\n    serial::text like $1 or uuid like $1 or notes like $1 or\n    trinfo::json#>>'{\"code\"}' like $1 or\n    trinfo::json#>>'{\"name\"}' like $1 or\n    i_pkt::json#>>'{\"2\"}' like $1 or\n    i_pkt::json#>>'{\"3\"}' like $1 or\n    i_pkt::json#>>'{\"4\"}' like $1 or\n    i_pkt::json#>>'{\"11\"}' like $1 or\n    i_pkt::json#>>'{\"41\"}' like $1 or\n    i_pkt::json#>>'{\"42\"}' like $1 or\n    o_pkt::json#>>'{\"37\"}' like $1 or\n    i_pkt::json#>>'{\"merId\"}' like $1 or\n    i_pkt::json#>>'{\"termId\"}' like $1 or\n    i_pkt::json#>>'{\"accNo\"}' like $1 or\n    i_pkt::json#>>'{\"orderId\"}' like $1 or\n    o_pkt::json#>>'{\"queryId\"}' like $1\n)";
    params = [];
    params.push('%' + res.locals.keyword + '%');
    index = 2;
    chnl = req.body.chnl || req.query.chnl || '';
    if (chnl.length > 0) {
      where_clause += " and chnl = $" + index;
      index += 1;
      params.push(chnl);
      res.locals.chnl = chnl;
    }
    today = moment().format('YYYY-MM-DD');
    start_date = req.body.start_date || req.query.start_date || today;
    end_date = req.body.end_date || req.query.end_date || today;
    table_name = 'pay_trlist_hist';
    if (start_date === today && end_date === today) {
      table_name = 'pay_trlist';
    }
    where_clause += " and sys_time::date >= '" + start_date + "'";
    res.locals.start_date = start_date;
    where_clause += " and sys_time::date <= '" + end_date + "'";
    res.locals.end_date = end_date;
    trcode = req.body.trcode || req.query.trcode || '';
    if (trcode.length > 0) {
      where_clause += " and trinfo::json#>>'{\"code\"}' = '" + trcode + "'";
      res.locals.trcode = trcode;
    }
    incoming = req.body.incoming || req.query.incoming || '';
    if (incoming.length > 0) {
      where_clause += " and incoming = $" + index;
      index += 1;
      params.push(incoming === 'true');
      res.locals.incoming = incoming === 'true';
    }
    settl = req.body.settl || req.query.settl || '';
    if (settl.length > 0) {
      where_clause += " and trinfo::json#>>'{\"settl\"}' = $" + index;
      index += 1;
      params.push(settl === 'true');
      res.locals.settl = settl === 'true';
    }
    succed = req.body.succed || req.query.succed || '';
    if (succed.length > 0) {
      if (succed === 'true') {
        where_clause += " and status = 0 and\ncase\nwhen chnl in (1,4) then\n    ( o_pkt is not null and\n    json_extract_path_text(o_pkt, '39') = '00')\nwhen chnl = 3 then\n    ( n_pkt is not null and\n    json_extract_path_text(n_pkt, 'respCode') = '00')\nend";
      } else {
        where_clause += " and (status <> 0 or\ncase\nwhen chnl in (1,4) then\n    ( o_pkt is null or\n    json_extract_path_text(o_pkt, '39') <> '00')\nwhen chnl = 3 then\n    ( n_pkt is null or\n    json_extract_path_text(n_pkt, 'respCode') <> '00')\nend)";
      }
      res.locals.succed = succed;
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/trlist.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 105
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/trlist.coffee"
          });
          client.query("select count(*) as count from " + table_name + " where " + where_clause, params, __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 111
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.total = result.rows[0].count;
          params.push(res.locals.offset);
          params.push(res.locals.page_rows);
          offset_index = params.length - 1;
          limit_index = params.length;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/trlist.coffee"
            });
            client.query("select *, case when chnl in (1,4) then array[ json_extract_path_text(i_pkt, '42'), json_extract_path_text(i_pkt, '41'), json_extract_path_text(i_pkt, '4'), json_extract_path_text(i_pkt, '11'), json_extract_path_text(o_pkt, '15') ] when chnl = 3 then array[ json_extract_path_text(i_pkt, 'merId'), json_extract_path_text(i_pkt, 'termId'), json_extract_path_text(i_pkt, 'txnAmt'), json_extract_path_text(n_pkt, 'traceNo'), case when n_pkt is not null then json_extract_path_text(n_pkt, 'settleDate') else json_extract_path_text(i_pkt, 'settleDate') end ] end as trdata from " + table_name + " where " + where_clause + " order by serial desc offset $" + offset_index + "::int limit $" + limit_index + "::int", params, __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 147
            }));
            __iced_deferrals._fulfill();
          })(function() {
            var _i, _len, _ref;
            if (err) {
              done(client);
              return next(err);
            }
            done();
            fmt_pan = function(pri_acct) {
              var len;
              if (!pri_acct || pri_acct.length < 9) {
                return pri_acct;
              }
              len = pri_acct.length;
              return pri_acct = pri_acct.substring(0, 4) + ' .. ' + pri_acct.substring(len - 4, len);
            };
            _ref = result.rows;
            for (_i = 0, _len = _ref.length; _i < _len; _i++) {
              record = _ref[_i];
              switch (record.chnl) {
                case 1:
                case 4:
                  if (record.i_pkt['2']) {
                    record.pri_acct = fmt_pan(record.i_pkt['2']);
                    break;
                  }
                  if (record.o_pkt && record.o_pkt['2']) {
                    record.pri_acct = fmt_pan(record.o_pkt['2']);
                    break;
                  }
                  if (record.i_pkt['36']) {
                    record.pri_acct = record.i_pkt['36'].substring(2, 10) + ' ...';
                    break;
                  }
                  if (record.i_pkt['35']) {
                    record.pri_acct = record.i_pkt['35'].substring(0, 8) + ' ...';
                    break;
                  }
                  break;
                case 3:
                  if (record.i_pkt.accNo) {
                    record.pri_acct = fmt_pan(record.i_pkt.accNo);
                  }
              }
            }
            return res.render('trlist/index', {
              records: result.rows
            });
          });
        });
      };
    })(this));
  });

}).call(this);
